package cn.jussi.sessiontest;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.jussi.utils.Captcha;

@WebServlet("*.sdo")
public class SessionTestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String mn = request.getServletPath();
		mn = mn.substring(1, mn.length()-4);
		try {
			Method method = this.getClass().getDeclaredMethod(mn, HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	
	@SuppressWarnings("unused")
	private void sessionTestServlet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/**
		 * 模拟网络不畅
		 */
		/*try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		/**
		 * 防止表单重复提交的方法（按返回，不刷新，再次按提交）
		 */
		String token = request.getParameter("token");
		String username = request.getParameter("username");
		String pasword = request.getParameter("pasword");
		HttpSession session = request.getSession();
		String sessionUUID = (String) session.getAttribute("uuid");
		session.removeAttribute("uuid");
		//System.out.println(token);
		//System.out.println(sessionUUID);	
		if(token.equals(sessionUUID)) {
			System.out.println("合法的请求，是第一次提交表单");
		} else {
			System.out.println("重复提交！");
		}
		
		/**
		 * 判断验证码是否输入正确（反正机器注册）
		 */
		String cc = request.getParameter("checkCode");		//用户自己填写的验证码，通过http请求传过来的
		String scc = (String)session.getAttribute("checkCode"); //从session中取出来的验证码
		if(!cc.equals(scc)) {
			System.out.println("验证码输入有误！");
			return; //终止本次程序的继续执行
		}
	}
	
	/**
	 * 生成验证码的方法
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@SuppressWarnings("unused")
	private void drawCheckCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("image/jpg");
		int width = 100;
		int height = 30;
		int num = 4;
		String code = "0123456789zxcvbnmasdfghjklqwert";
		//具体画验证码
		Captcha captcha = Captcha.getInstance();
		captcha.set(width, height, num, code);
		String cc = captcha.generateCheckCode();
		HttpSession session = request.getSession();
		session.setAttribute("checkCode", cc);
		OutputStream os = response.getOutputStream();
		ImageIO.write(captcha.generateCheckImg(cc), "jpg", os);
	}
}
